package bstsetcode;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/15 11:27
 * 递归求和 ：
 *  求{1,2,3,4,5,6,7,}和
 *  转化为： 求 sum(arr[0...n])==> arr[0]+ sum(arr[1..n])
 */
public class Sum {

    public static int sum(int[] arr){
        return sum(arr,0);
    }

    private static int sum(int[] arr, int l) {
        //整个数组为null  就是出口
        if(l == arr.length){
            return 0;
        }
        return arr[l] +sum(arr,l+1);
    }

    public static void main(String[] args) {
        int[]  nums = {1,2,3,4,5,6,7,8};
        System.out.println(sum(nums));
    }
}
